capture log close
set more off
//cd -- Change working directory if you don't want to 
*hardcode in the log and data locations
cd "C:\Users\A02177653\Dropbox\primary strategies\work\data\management"

log using "9replicationfile_v4_071717",replace text

//  program:	replication
//  task:       this file uses the models run in 3regressionmodels121316 and 6creatinghazarddat_042017to replicate all results in paper created for R&R to APR
//	related files: 	3regressionmodels121316		
//	project:    Primaries and Momentum
//  author:     Josh Ryan  Summer 17

// #Start
// program setup

version 11.1
clear all 
set linesize 80
macro drop _all
set seed 123456

*revised dataset
use "4datav2071717.dta", clear
xtset canid pordercenter


*Table2--The Conditional Effect of Party on Avg. Vote Share.
*model 1: base model just including all control variables
reg avgdaywin c.l.avgdaywin c.l.canwinperday cancount primratio moneyper stateperday specialday i.partyid, cluster(canid)
estimates store base1

*interacting avgdaywin with partyid-this is not significant, demonstrates no bonus just for doing well, but bonus for winning, evidence that rules matter
reg avgdaywin c.l.avgdaywin##i.partyid c.l.canwinperday cancount primratio moneyper stateperday specialday, cluster(canid)
estimates store base2
margins, dydx(l.avgdaywin) at(partyid=(0 1)) 

*interacted with party
reg avgdaywin c.l.avgdaywin i.partyid##c.l.canwinperday cancount primratio moneyper stateperday specialday, cluster(canid)
estimates store base3
margins, dydx(l.canwinperday) at(partyid=(0 1)) 

*interacted with party, anywin
reg avgdaywin c.l.avgdaywin i.partyid##c.l.anywin cancount primratio moneyper stateperday specialday, cluster(canid)
estimates store base4
margins, dydx(l.anywin) at(partyid=(0 1)) 

estout1 base1 base2 base3 base4 , style(tex) star(0.10 0.05) se(par) stats(aic N rsq)

*mentioned in text, not in table
reg avgdaywin c.l.avgdaywin##i.specialday c.l.canwinperday cancount primratio moneyper stateperday i.partyid, cluster(canid)
margins, dydx(l.avgdaywin) at(specialday=(0 1))

 reg avgdaywin c.l.avgdaywin i.partyid##i.specialday c.l.canwinperday cancount primratio moneyper stateperday, cluster(canid)
margins, dydx(l.avgdaywin) at(partyid=(0 1))


*Table 3--Poisson Models of the Number of States Won
poisson canwinperday c.l.avgdaywin##i.partyid cancount primratio moneyper stateperday specialday, irr cluster(canid) 
estimates store poisson1
margins, dydx(l.avgdaywin) at(partyid=(0 1)) 

poisson canwinperday c.l.avgdaywin c.l.canwinperday##i.partyid cancount primratio moneyper stateperday specialday, cluster(canid)
estimates store poisson2

poisson canwinperday  c.l.avgdaywin i.l.anywin##i.partyid cancount primratio moneyper stateperday specialday, irr cluster(canid)
estimates store poisson3 
margins, dydx(i.l.anywin) at(partyid=(0 1))

estout1 poisson1 poisson2 poisson3  , style(tex) star(0.10 0.05) se(par) stats(aic N rsq)

*Table 4 - estimates of effect of winner take all on success
reg avgdaywin c.l.avgdaywin i.l.wtadummy c.l.canwinperday cancount primratio moneyper stateperday specialday i.partyid, cluster(canid)
estimates store wta1

reg avgdaywin c.l.avgdaywin##i.l.wtadummy c.l.canwinperday cancount primratio moneyper stateperday specialday i.partyid, cluster(canid)
estimates store wta2
margins, dydx(l.avgdaywin) at(l.wtadummy=(0 1)) 

poisson canwinperday c.l.avgdaywin i.l.wtadummy i.partyid cancount primratio moneyper stateperday specialday, cluster(canid) irr
estimates store wta3

poisson canwinperday c.l.avgdaywin##i.l.wtadummy i.partyid cancount primratio moneyper stateperday specialday, cluster(canid) irr
margins, dydx(l.avgdaywin) at(l.wtadummy=(0 1)) 
estimates store wta4

estout1 wta1 wta2 wta3 wta4, style(tex) star(0.10 0.05) se(par) stats(aic N rsq)


use "1hazarddatav2071717.dta", clear
set seed 123456

gen t0=0

stset pordercenter, failure(outcome) time(t0)

*base model--Appendix Table A--model with grambsch-therneau test
stcox avgdaywin finishmean c.canperstate  ///
i.partyid primratio moneyper stateperday specialday, nohr vce(robust) schoenfeld(sch*) scaledsch(sca*)
stphtest, rank detail
drop sch*
drop sca*

*model with win rather than average suppor, Appendix table B
stcox anywin finishmean c.canperstate partyid  ///
primratio moneyper stateperday specialday, nohr vce(robust) schoenfeld(sch*) scaledsch(sca*)
stphtest, rank detail
drop sch*
drop sca*

*models with interactions for time to correct proporational hazards violations
stcox avgdaywin finishmean c.canperstate  ///
partyid primratio moneyper stateperday specialday, nohr vce(robust) tvc(partyid primratio ///
specialday) texp(_t)
estimates store hazard1
test _b[main:partyid]+_b[tvc:partyid] = 0

stcox anywin finishmean c.canperstate partyid  ///
 primratio moneyper stateperday specialday, nohr vce(robust) tvc(anywin c.canperstate partyid primratio ///
 stateperday specialday) texp(_t)
 estimates store hazard2
 test _b[main:partyid]+_b[tvc:partyid] = 0
 
 estout1 hazard1 hazard2, style(tex) star(0.10 0.05) se(par) stats(aic N rsq)

 
*Figure Appendix C:
 sts graph, by(partyid) ylabel(0(.1)1, nogrid) xlabel(0(2)30) ///
ytitle("Estimated Survivor Function")  title("") ///
xtitle("Analysis Time") plot1opts(lpattern("-" "--") lcolor(black) lwidth(medium)) ///
legend(col(1) order(1 "Democrats" 2 "Republicans") ///
position(6) ring(0) size(small)) scheme(s1mono) graphregion(fcolor(white) ifcolor(white))
graph save km-curve.gph, replace

graph export "C:\Users\A02177653\Dropbox\primary strategies\work\tex\km-curve.pdf", replace

 
 *substantive graph 
*load the data generated from model 1 of Table XX
use "hazardeffect_v3_072017.dta", clear

twoway (line hazard t, sort lcolor(black) lpattern(solid) lwidth(thick)) if t<21, ///
ytitle("Estimated Hazard of Republican Party Candidate") xtitle("Primary Date Order") ///
 ylabel(5(.5)0, valuelabel) ///
xlabel(1(1)20, valuelabel) ///
yline(1) ///
legend(off) ///
scheme(s1mono) graphregion(fcolor(white) ifcolor(white)) title()
graph save hazardeffect.gph, replace

graph export "C:\Users\A02177653\Dropbox\primary strategies\work\tex\hazardeffect.pdf", replace


use "4datav2071717.dta", clear
xtset canid pordercenter

*Table 5 - conditional effect of money and parties
*is there a difference between the parties?
reg avgdaywin c.l.avgdaywin c.moneyper##i.partyid cancount primratio moneyper stateperday specialday, cluster(canid)
estimates store money1
margins, dydx(c.moneyper) at(partyid=(0 1)) 

poisson canwinperday c.l.avgdaywin c.moneyper##i.partyid cancount primratio moneyper stateperday specialday, cluster(canid)
estimates store money2
margins, dydx(c.moneyper) at(partyid=(0 1)) 

 estout1 money1 money2, style(tex) star(0.10 0.05) se(par) stats(aic N rsq)
 
 
 
  *These results referenced in text, but not shown
 *generate year dummy, 2008 and beyond
gen year04=0
replace year04=1 if year>2004

reg avgdaywin c.l.avgdaywin c.moneyper##i.partyid cancount primratio moneyper stateperday specialday if year04==1, cluster(canid)
margins, dydx(c.moneyper) at(partyid=(0 1)) 

poisson canwinperday c.l.avgdaywin c.moneyper##i.partyid cancount primratio moneyper stateperday specialday if year04==1, cluster(canid)
estimates store money2
margins, dydx(c.moneyper) at(partyid=(0 1)) 

 estout1 money1 money2, style(tex) star(0.10 0.05) se(par) stats(aic N rsq)


 *appendix D
  gen recent=0
 replace recent=1 if year>=2008

reg avgdaywin c.l.avgdaywin i.partyid##c.year cancount primratio moneyper stateperday specialday, cluster(canid)
estimates store year1
margins, dydx(c.year) at(partyid=(0 1)) 

poisson canwinperday c.l.avgdaywin c.year##i.partyid cancount primratio moneyper stateperday specialday, cluster(canid)
estimates store year2
margins, dydx(c.year) at(partyid=(0 1)) 

reg avgdaywin c.l.avgdaywin i.partyid##c.l.avgdaywin cancount primratio moneyper stateperday specialday if recent==1, cluster(canid)
estimates store recent1
margins, dydx(c.l.avgdaywin) at(partyid=(0 1)) 

 poisson canwinperday c.l.canwinperda##i.partyid i.recent cancount primratio moneyper stateperday specialday if recent==1, cluster(canid)
estimates store recent2
*margins, dydx(c.l.canwinperday) at(partyid=(0 1))

estout1 year1 year2 recent1 recent2, style(tex) star(0.10 0.05) se(par) stats(aic N rsq)

 
 
 
 




